package cn.rocky.offer;

import java.util.LinkedList;
import java.util.Stack;

/**
 * @version 1.0
 * @description 输入两个整数序列，第一个序列表示栈的压入顺序，请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。
 * 例如，序列 {1,2,3,4,5} 是某栈的压栈序列，序列 {4,5,3,2,1} 是该压栈序列对应的一个弹出序列，
 * 但 {4,3,5,1,2} 就不可能是该压栈序列的弹出序列。
 * @author viki
 * @date 2021/7/10 18:02
 */

class BooleanStackSequences {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack = new Stack<Integer>();
        Integer i = 0;
        for (int num:pushed) {
            // 入栈
            stack.push(num);
            // 循环判断与出栈
            while (!stack.isEmpty() && stack.peek() ==popped[i]) {
                stack.pop();
                i++;
            }
        }
        return stack.isEmpty() ? true : false;
    }
}

/**
 * @author Rocky
 */
public class offer31 {
    public static void main(String[] args) {
        // 定义入栈和出栈
        int[] pushed = new int[]{1,2,3,4,5};
        int[] popped = new int[]{1,2,3,4,5};
        int[] popped1 = new int[]{2,1,3,4,5};
        int[] popped2 = new int[]{3,2,1,4,5};
        int[] popped3 = new int[]{4,5,2,1,3};
        // 判断入栈和出栈是否一致
        BooleanStackSequences booleanStackSequences = new BooleanStackSequences();
        boolean b = booleanStackSequences.validateStackSequences(pushed, popped);
        boolean c = booleanStackSequences.validateStackSequences(pushed, popped1);
        boolean d = booleanStackSequences.validateStackSequences(pushed, popped2);
        boolean e = booleanStackSequences.validateStackSequences(pushed, popped3);
        // 打印值
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
        System.out.println(e);

        // LinkedList和Stack push的区别
        // @TODO 想要知道 LinkedList和Stack push的区别,目前只是打印地址,希望把值打印出来
        System.out.println("----");
        LinkedList linkedList = new LinkedList();
        linkedList.push(pushed);
        System.out.println("LinkedList输出: " +  linkedList);
        Stack stack = new Stack();
        stack.push(pushed);
        System.out.println("Stack输出: " +  stack);
    }
}
